/**
 * Created by Administrator on 2017/1/16.
 */

var pomelo = require('pomelo');
var roomManager = require('../../../logic/game/roomManager');

module.exports = function() {
    return new Filter();
};

var Filter = function() {
};

Filter.prototype.before = function(msg, session, next) {
    delete msg.nolog;

    var room = roomManager.getRoom(session.get('rid'));
    if (!room) {
        next(new Error('Room not exist!'));
        return;
    }

    var player = room.getPlayerByUid(session.uid);
    if (!player) {
        next(new Error('Room player not exist!'));
        return;
    }

    session.room = room;
    session.player = player;

    next();
};

Filter.prototype.after = function(err, msg, session, resp, next) {
    if (msg.nolog) {
        delete msg.nolog;
    } else {
        if (err) {
            logger.info("room=%d index=%d handler %j err=%j", session.get('rid'), session.uid, msg, err);
        } else {
            logger.debug("room=%d index=%d handler %j resp=%j", session.room.rid(), session.player.getIndex(), msg, resp);
        }
    }

    next();
}